import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store/index.js'
/* import Login from '@/views/login/index.vue'
import Layout from '@/views/Layout/index.vue'
import Home from '@/views/Home/index.vue'
import User from '@/views/User/index.vue'
import Search from '@/views/Home/search.vue'
import SearchResult from '@/views/Home/components/searchArticle/SearchResult.vue'
import ArticleDetail from '@/views/ArticleDetail/articleDetail.vue'
import UserEdit from '@/views/User/component/userEdit.vue'
import Chat from '@/views/User/component/chat.vue' */
// 使用懒加载 可以减少不必要的加载 节省主页加载事件 根据不同页面进行加载分块

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/layout'
  },
  {
    path: '/login',
    component: () => import('@/views/login/index.vue')
  },
  {
    path: '/layout',
    component: () => import('@/views/Layout/index.vue'),
    redirect: '/layout/home',
    children: [
      {
        path: 'home',
        component: () => import('@/views/Home/index.vue'),
        meta: { isRecord: true, top: 0 }
      },
      {
        path: 'user',
        component: () => import('@/views/User/index.vue')
      }
    ]
  },
  { path: '/search', component: () => import('@/views/Home/search.vue') },
  { path: '/search/:keyword', component: () => import('@/views/Home/components/searchArticle/SearchResult.vue') },
  { path: '/art_detail/:aid', component: () => import('@/views/ArticleDetail/articleDetail.vue') },
  { path: '/user/edit', component: () => import('@/views/User/component/userEdit.vue') },
  { path: '/chat', component: () => import('@/views/User/component/chat.vue') }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  if (store.state.token.length > 0 && to.path === '/login') {
    next(false)
  } else {
    next()
  }
})
router.afterEach((to, from) => {
  if (to.meta.isRecord) {
    setTimeout(() => {
      window.scrollTo(0, to.meta.top)
    })
  }
})

export default router
